package LUCKY.Android.Util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.zip.GZIPInputStream;

import org.kobjects.base64.Base64;

import android.util.Log;

public class GZipUtil {

	public static String Decompress(String text)  {
	    try {
			int size = 0;
			byte[] gzipBuff = Base64.decode(text);

			ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff, 4,
			        gzipBuff.length - 4);
			GZIPInputStream gzin = new GZIPInputStream(memstream);

			final int buffSize = 8192;
			byte[] tempBuffer = new byte[buffSize];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((size = gzin.read(tempBuffer, 0, buffSize)) != -1) {
			    baos.write(tempBuffer, 0, size);
			}
			byte[] buffer = baos.toByteArray();
			baos.close();

			return new String(buffer, "UTF-8");
		} catch (IOException e) {
			Log.e("Error", e.getMessage());
			return null;
		}
	}
	
	
	public static String Decompress2(String text)  {
	    final int BUFFER_SIZE = 8192;
	    
	    try {
			byte[] compressed = Base64.decode(text);
			
			ByteArrayInputStream is = new ByteArrayInputStream(compressed);
			GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
			InputStreamReader reader = new InputStreamReader(gis);
			//StringBuilder string = new StringBuilder();
			StringWriter stringw = new StringWriter();
			char[] data = new char[BUFFER_SIZE];
			int bytesRead;
			while ((bytesRead = reader.read(data)) != -1) {
			    //string.append(new String(data, 0, bytesRead));
				stringw.write(data, 0, bytesRead);
			}
			reader.close();
			gis.close();
			is.close();
			return stringw.toString();
		}  catch (IOException e) {
			Log.e("Decompress2", e.getMessage());
			return null;
		}
	}
	
}

